layui.use(['form','treeSelect','layer','upload','laydate','layedit'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    var upload = layui.upload;
    var treeSelect = layui.treeSelect;

    //-----------
    var newsDetail;
    layui.use('layedit', function(){
        var layedit = layui.layedit;
        layedit.set({
            uploadImage: {
                url: '/upload/editPicUpload' //接口url
                ,type: 'post' //默认post
            }
        });
        newsDetail=layedit.build('newsDetail',{
            height: 160 //设置编辑器高度
        }); //建立编辑器
    });

    //-----------


    //回显数据
    var request = GetRequest();
     $.get("/news/" + request.id,function (data) {
        $("#id").val(data.data.news.id);

        $("#title").val(data.data.news.title);
        $("#source").val(data.data.news.source);
        $("#brief").val(data.data.news.brief);
        // $("#isHot input[value="+data.data.news.isHot+"]").prop("checked","checked");  //热门与否
        $("#isHot input[value="+data.data.news.isHot+"]").prop("checked",true);
        form.render('radio');

        $("#uploadImage").attr("src",data.data.news.pic);//图片
        $("#photo").attr("value",data.data.news.pic);

        $("#newsDetail").text(data.data.news.newsDetail);
         newsDetail=layui.layedit.build('newsDetail',{
             height: 160 //设置编辑器高度
         });
        // $("#maxValue").val(data.maxValue);

        $("#remarks").val(data.data.news.remarks);
        $("#createBy").val(data.data.news.createBy);

        //检测项目数据
        $.get("/newsType/all",function (newsTypeId) {
            $.each(newsTypeId.data,function (i,ele) {
                if (data.data.news.newsTypeId == ele.id) {
                    $("#newsTypeId").append('<option selected="selected" value="'+ ele.id +'">'+ ele.name +'</option>');
                } else {
                    $("#newsTypeId").append('<option value="'+ ele.id +'">'+ ele.name +'</option>');
                }
            });
            form.render('select');
        });
    });


    form.on("submit(edit)",function(data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        // 实际使用时的提交信息
        $.post("/news/edit",{
            id : $("#id").val(),
            title : $("#title").val(),  //新闻标题
            newsTypeId : $("#newsTypeId").val(), //新闻类型
            source : $("#source").val(),  //新闻来源
            brief : $("#brief").val(),  //新闻类型
            pic : $("#photo").val(),
            isHot: $("#isHot").children("input:checked").val(),  //新闻热门
            newsDetail: layui.layedit.getContent(newsDetail),
            createBy: $("#createBy").val(),
            remarks: $("#remarks").val(),

        },function(res){
            //
        })
        setTimeout(function(){
            top.layer.close(index);
            top.layer.msg("新闻添加成功！");
            layer.closeAll("iframe");
            //刷新父页面
            parent.location.reload();
        },2000);
        return false;
    })

    var upload = layui.upload;
    //上传头像

    upload.render({
        elem: '.userFaceBtn',
        field: 'uploadFile',//上传文件的名称，要和接口参数名称相同
        url: '/upload/pic',
        //multiple:true,
        done: function(res, index, upload){
            if (res.code === 0) {
                layer.msg(res.msg);
                $("#uploadImage").attr("src",res.url);
                $("#photo").val(res.url);
            } else {
                layer.msg(res.msg);
            }
        }
    });
    //获取路径中的参数
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }


});